package dao.hibernate;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.iterators.EntrySetMapIterator;
import org.hibernate.Query;

import util.Util;

import dao.DiaDiemYTeDAO;
import model.ChuDe;
import model.DiaDiemYTe;
import model.LoaiDiaDiemYTe;

public class DiaDiemYTeDAOHibernate extends GenericHibernateDAO<DiaDiemYTe, Long> implements DiaDiemYTeDAO{
	@SuppressWarnings("unchecked")
	@Override
	public List<DiaDiemYTe> findDiaDiemYTeByLoaiDiaDiem(String diaDiem) {
		List<DiaDiemYTe> list = new ArrayList<DiaDiemYTe>();
		Query query = getSession().createQuery("SELECT diaDiem.idChuDe, diaDiem.tenChuDe, diaDiem.chuDeCha, diaDiem.loaiDiaDiemYTe, diaDiem.diaChi, diaDiem.soDienThoai, diaDiem.viDo, diaDiem.kinhDo FROM DiaDiemYTe AS diaDiem WHERE diadiem.loaiDiaDiemYTe.tenLoaiDiaDiemYTe = :pname ORDER BY diaDiem.tenChuDe")
		.setString("pname", diaDiem);
		Iterator iter = query.list().iterator();
		DiaDiemYTe diaDiemYTe = null;
		while(iter.hasNext()){
			Object[] row = (Object[])iter.next();
			long idDiaDiem = (Long)row[0];
			String tenDiaDiem = (String)row[1];
			ChuDe chuDeCha = (ChuDe)row[2];
			LoaiDiaDiemYTe loaiDiaDiem = (LoaiDiaDiemYTe)row[3];
			String diaChi = (String)row[4];
			String soDienThoai = (String)row[5];
			float viDo = (Float)row[6];
			float kinhDo = (Float)row[7];
			diaDiemYTe = new DiaDiemYTe(tenDiaDiem, chuDeCha, loaiDiaDiem, null, diaChi, soDienThoai, viDo, kinhDo);
			diaDiemYTe.setIdChuDe(idDiaDiem);
			list.add(diaDiemYTe);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DiaDiemYTe> findDiaDiemYTeByIdLoaiDiaDiem(long id) {
		List<DiaDiemYTe> list = new ArrayList<DiaDiemYTe>();
		Query query = getSession().createQuery("SELECT diaDiem.idChuDe, diaDiem.tenChuDe, diaDiem.chuDeCha, diaDiem.loaiDiaDiemYTe, diaDiem.diaChi, diaDiem.soDienThoai, diaDiem.viDo, diaDiem.kinhDo FROM DiaDiemYTe AS diaDiem WHERE diadiem.loaiDiaDiemYTe.idLoaiDiaDiemYTe = :pname ORDER BY diaDiem.tenChuDe")
		.setLong("pname", id);
		Iterator iter = query.list().iterator();
		DiaDiemYTe diaDiemYTe = null;
		while(iter.hasNext()){
			Object[] row = (Object[])iter.next();
			long idDiaDiem = (Long)row[0];
			String tenDiaDiem = (String)row[1];
			ChuDe chuDeCha = (ChuDe)row[2];
			LoaiDiaDiemYTe loaiDiaDiem = (LoaiDiaDiemYTe)row[3];
			String diaChi = (String)row[4];
			String soDienThoai = (String)row[5];
			float viDo = (Float)row[6];
			float kinhDo = (Float)row[7];
			diaDiemYTe = new DiaDiemYTe(tenDiaDiem, chuDeCha, loaiDiaDiem, null, diaChi, soDienThoai, viDo, kinhDo);
			diaDiemYTe.setIdChuDe(idDiaDiem);
			list.add(diaDiemYTe);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<DiaDiemYTe> findByTenDiaDiemLike(String name) {
		return getSession()
				.createQuery(
						"FROM DiaDiemYTe AS diaDiemYTe WHERE diaDiemYTe.tenChuDe LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DiaDiemYTe> findByThongTin(String name) {
		return getSession()
				.createQuery(
						"FROM DiaDiemYTe AS diaDiemYTe WHERE diaDiemYTe.thongTin LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DiaDiemYTe> findByDiaChiLike(String name) {
		return getSession()
				.createQuery(
						"FROM DiaDiemYTe AS diaDiemYTe WHERE diaDiemYTe.diaChi LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DiaDiemYTe> findBySoDienThoaiLike(String name) {
		return getSession()
				.createQuery(
						"FROM DiaDiemYTe AS diaDiemYTe WHERE diaDiemYTe.soDienThoai LIKE :pname")
				.setString("pname", "%" + name + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DiaDiemYTe> findByManyConditions(MultiMap conditions) {
		List<DiaDiemYTe> result = null;
		EntrySetMapIterator iterator = new EntrySetMapIterator(conditions);
		String criteria = "";
		List<String> keywords = new ArrayList<String>();
		String keyword = "";
		while (iterator.hasNext()) {
			criteria = (String) iterator.next();
			keywords = (ArrayList<String>) conditions.get(criteria);
			for (String k : keywords) {
				keyword = k;
				if (criteria.equals("tenDiaDiemYTe")) {
					List<DiaDiemYTe> list = findByTenDiaDiemLike(keyword);
					result = Util.retainAllDiaDiemYTe(result, list);
				} else if (criteria.equals("thongTin")) {
					List<DiaDiemYTe> list = findByThongTin(keyword);
					result = Util.retainAllDiaDiemYTe(result, list);
				} else if (criteria.equals("diaChi")) {
					List<DiaDiemYTe> list = findByDiaChiLike(keyword);
					result = Util.retainAllDiaDiemYTe(result, list);
				} else if (criteria.equals("soDienThoai")) {
					List<DiaDiemYTe> list = findBySoDienThoaiLike(keyword);
					result = Util.retainAllDiaDiemYTe(result, list);
				}
			}
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<DiaDiemYTe> findByTenLoaiDiaDiemYTeLike(String keyword) {
		return getSession()
		.createQuery(
				"FROM DiaDiemYTe AS diadiem WHERE diadiem.loaiDiaDiemYTe.tenLoaiDiaDiemYTe LIKE :pname")
		.setString("pname", "%" + keyword + "%").list();
	}

	@Override
	public List<DiaDiemYTe> findByOneCondition(String criteria, String keyword) {
		List<DiaDiemYTe> list = new ArrayList<DiaDiemYTe>();
		if (criteria.equals("tenDiaDiemYTe")) {
			list = findByTenDiaDiemLike(keyword);
		} else if (criteria.equals("loaiDiaDiemYTe")) {
			list = findByTenLoaiDiaDiemYTeLike(keyword);
		} else if (criteria.equals("diaChi")) {
			list = findByDiaChiLike(keyword);
		} else if (criteria.equals("soDienThoai")) {
			list = findBySoDienThoaiLike(keyword);
		}
		
		List<DiaDiemYTe> result = new ArrayList<DiaDiemYTe>();
		DiaDiemYTe diaDiemYTeSimple = null;
		for (DiaDiemYTe diaDiem : list) {
			diaDiemYTeSimple = new DiaDiemYTe(diaDiem.getTenChuDe(), diaDiem.getChuDeCha(), diaDiem.getLoaiDiaDiemYTe(), null, diaDiem.getDiaChi(), diaDiem.getSoDienThoai(), diaDiem.getViDo(), diaDiem.getKinhDo());
			diaDiemYTeSimple.setIdChuDe(diaDiem.getIdChuDe());
			result.add(diaDiemYTeSimple);
		}
		
		return result;
	}

}
